const sequelize = require("../sequelize");
const Manager = require("./manager/manager");
const Role = require("./role/role");
const RoleRule = require("./role/role_rule");
const Rule = require("./rule/rule");
const User = require("./user/user");


const GoodsAttrs = require("./goods/goods_attrs");
const Goods = require("./goods/goods");
const GoodsBanner = require("./goods/goods_banner");
const Category = require("./category/category");
const OrderItem = require("./order/order_item");
const GoodsSkus = require("./goods/goods_skus");
const GoodsSkusCard = require("./goods/goods_skus_card");
const { Op } = require("sequelize");
const ImageClass = require("./image/image_class");
const Image = require("./image/image");
const GoodsType = require("./goods/goods_type");
const GoodsTypeValue = require("./goods/goods_type_value");
const GoodsSkusCardValue = require("./goods/goods_skus_card_value");
const UserLevel = require("./user/user_level");
const UserInfo = require("./user/user_info");
const UserAddresses = require("./user/user_addresses");
const Order = require("./order/order");
const CouponUser = require("./coupon/coupon_user");
const Cart = require("./cart/cart");
const Coupon = require("./coupon/coupon");
//创建商品关系
async function initGoods() {
    /**
     * 关系笔记
     * hasMany是一 belongsTo是多
     * hasMany里可以设置belongsTo表关联的字段
     */
    //创建商品与分类多对一 
    Category.hasMany(Goods, { foreignKey: "category_id" })
    Goods.belongsTo(Category, { foreignKey: "category_id" })
    //关联商品与评论一对多
    OrderItem.belongsTo(Goods, { foreignKey: "goods_id",as:"goods_item" })
    Goods.hasMany(OrderItem, { foreignKey: "goods_id" })
    //关联商品与规格一对多
    GoodsSkus.belongsTo(Goods, { foreignKey: "goods_id" })
    Goods.hasMany(GoodsSkus, { foreignKey: "goods_id" })
    //商品与规格卡片一对多
    GoodsSkusCard.belongsTo(Goods, { foreignKey: "goods_id" })
    Goods.hasMany(GoodsSkusCard, { foreignKey: "goods_id" })
    //规格卡片与卡片值一对多
    GoodsSkusCardValue.belongsTo(GoodsSkusCard,{foreignKey:"goods_skus_card_id"})
    GoodsSkusCard.hasMany(GoodsSkusCardValue,{foreignKey:"goods_skus_card_id"})

    //创建商品与商品属性一对多
    GoodsAttrs.belongsTo(Goods, { foreignKey: "goods_id" })
    Goods.hasMany(GoodsAttrs, { foreignKey: "goods_id" })
    //创建商品与商品轮播图一对多
    GoodsBanner.belongsTo(Goods, { foreignKey: "goods_id" })
    Goods.hasMany(GoodsBanner, { foreignKey: "goods_id" })
    //商品与goods_type多对一
    Goods.belongsTo(GoodsType, { foreignKey: "type_id" })
    GoodsType.hasMany(Goods, { foreignKey: "type_id" })
    //goods_type与goods_type_value
    GoodsTypeValue.belongsTo(GoodsType, { foreignKey: "goods_type_id" })
    GoodsType.hasMany(GoodsTypeValue, { foreignKey: "goods_type_id" })

}
//创建图片与图片类型关系
async function initImage() {
    Image.belongsTo(ImageClass, { foreignKey: "image_class_id" })
    ImageClass.hasMany(Image, { foreignKey: "image_class_id" })
}
//初始化用户表关联
async function initUser(params) {
    //与用户等级关联
    User.belongsTo(UserLevel,{foreignKey:"user_level_id"})
    UserLevel.hasMany(User,{foreignKey:"user_level_id"})
    //与资料关联
    User.hasOne(UserInfo)
    UserInfo.belongsTo(User)
    //与收货地址列表关联
    User.hasMany(UserAddresses,{foreignKey:"user_id"})
    UserAddresses.belongsTo(User,{foreignKey:"user_id"})
    //关联购物车
    User.hasMany(Cart,{foreignKey:"user_id"})
    Cart.belongsTo(User,{foreignKey:"user_id"})
    {
        //购物车关联商品规格
        Cart.belongsTo(GoodsSkus,{foreignKey:"goods_skus_id"})
        GoodsSkus.hasMany(Cart,{foreignKey:'goods_skus_id'})
    }
    //关联订单
    User.hasMany(Order,{foreignKey:"user_id"})
    Order.belongsTo(User,{foreignKey:"user_id"})
    //关联优惠劵
    User.hasMany(CouponUser,{foreignKey:"user_id"})
    CouponUser.belongsTo(User,{foreignKey:"user_id"})
    
}
//初始化订单关系
async function initOrder()     {
    //关联订单商品
    Order.hasMany(OrderItem,{foreignKey:"order_id"})
    OrderItem.belongsTo(Order,{foreignKey:"order_id"})
    //关联优惠劵
    Order.hasOne(CouponUser)
    CouponUser.belongsTo(Order)
    //关联收货地址
    Order.belongsTo(UserAddresses)
    UserAddresses.hasMany(Order,)
}
//初始化评论模型
async function initOrderItem(){
    OrderItem.belongsTo(User,{foreignKey:"user_id"})
    User.hasMany(OrderItem,{foreignKey:"user_id"})

    OrderItem.belongsTo(GoodsSkus,{foreignKey:"shop_id"})
    GoodsSkus.hasMany(OrderItem,{foreignKey:"shop_id"})


}
//初始化优惠劵关系
async function initCoupon(){
    //关联领取情况
    Coupon.hasMany(CouponUser)
    CouponUser.belongsTo(Coupon)
}
//初始化关系
module.exports = async function init() {
    //创建管理员与角色的多对一关系
    Manager.belongsTo(Role, { foreignKey: "role_id" })
    Role.hasMany(Manager, { foreignKey: "role_id" })

    //创建角色与权限的多对多关系
    Role.belongsToMany(Rule, { through: RoleRule, foreignKey: "role_id" })
    Rule.belongsToMany(Role, { through: RoleRule, foreignKey: "rule_id" })

    await initGoods()
    await initImage()
    await initUser()
    await initOrder()
    await initOrderItem()
    await initCoupon()
    //初始化数据库
    await sequelize.sync({ force: false })
}
